﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using yncTech.BusinessObjects;
using yncTech.Common;


public partial class Admin_Fin_Fin001a : AdminBasePage
{
    decimal? receiptID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[FinReceiptMetadata.ColumnNames.ReceiptID] != null)
        {
            receiptID = decimal.Parse(Request.QueryString[FinReceiptMetadata.ColumnNames.ReceiptID]);
        }
        if (!IsPostBack)
        {
            switch (ActionType)
            {
                case OperationType.Add:
                    txtInDate.Value = DateTime.Now.ToString(ConstantValue.DateFormat);
                    txtVoucherDate.Value = DateTime.Now.ToString(ConstantValue.DateFormat);
                    btnSave.Text = "新增";
                    break;
                case OperationType.Update:
                case OperationType.Delete:
                    if (receiptID.HasValue)
                    {
                        FinReceipt re = new FinReceipt();
                        re.LoadByPrimaryKey(receiptID.Value);
                        drpDocType.SelectedValue = re.DocumentsType;
                        txtDocumentNo.Text = re.DocumentsNo;
                        if (re.IsSAP.HasValue && re.IsSAP.Value)
                        {
                            drpDocType.Enabled = false;
                            txtDocumentNo.Enabled = false;
                        }

                        drpCompany.SelectedValue = re.CompanyCode;
                        drpDivision.SelectedValue = re.Division;

                        txtVocherDesc.Text = re.VoucherDescription;
                        txtVoucherDate.Value = re.VoucherDate;
                        txtInDate.Value = re.FirstStorageDate.FormatDate(ConstantValue.DateFormat);

                        drpFunctionDept.SelectedValue = re.FunctionDept;
                        drpLocation.SelectedValue = re.StorageLocation.ToString();
                        drpStorageStatus.SelectedValue = re.StorageStatus;
                        txtComments.Text = re.Comments;
                        if (re.BoxID > 0)
                        {
                            btnSave.Visible = false;
                        }
                    }
                    if (ActionType == OperationType.Update)
                    {
                        btnSave.Text = "修改";
                    }
                    else
                    {
                        btnSave.OnClientClick = "return confirm('您确定要删除凭证吗？');";
                        //btnSave.Attributes.Add("", "");
                        btnSave.Text = "删除";
                    }
                    panAuth.Visible = true;
                    break;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";

            //if (ActionType == OperationType.Delete || ActionType == OperationType.Update)
            //{
            //    if (txtAuthor.Text.Trim().Equals(LoginUserInfo.CurrentUser.LoginName)|| !LoginUserInfo.CheckUser(txtAuthor.Text,txtPwd.Text))
            //    {
            //        lbMessage.Text = "授权验证失败!";
            //        return;
            //    }
            //}

            FinReceipt receipt = new FinReceipt();


            if (ActionType != OperationType.Delete)
            {
                receipt.Query.Where(receipt.Query.DocumentsNo == txtDocumentNo.Text.Trim());
                if (ActionType == OperationType.Update)
                {
                    receipt.Query.Where(receipt.Query.ReceiptID != receiptID.Value);
                }
                receipt.Query.Load();
                if (receipt.es.HasData)
                {
                    lbMessage.Text = "凭证索引号已存在!!";
                    return;
                }
            }

            switch (ActionType)
            {
                case OperationType.Add:
                    receipt.DocumentsNo = txtDocumentNo.Text.Trim();
                    receipt.DocumentsType = drpDocType.SelectedValue;
                    receipt.StorageLocation = decimal.Parse(drpLocation.SelectedValue);
                    receipt.StorageStatus = drpStorageStatus.SelectedValue;
                    receipt.FunctionDept = drpFunctionDept.SelectedValue;
                    receipt.Comments = txtComments.Text;

                    receipt.Division = drpDivision.SelectedValue;
                    receipt.VoucherDate = txtVoucherDate.Value;
                    receipt.VoucherDescription = txtVocherDesc.Text;
                    receipt.FirstStorageDate = txtInDate.Value.ToDate();
                    receipt.CompanyCode = drpCompany.SelectedValue;
                    receipt.TransferRecord = 0;

                    receipt.CreatedDttm = DateTime.Now;
                    receipt.IsConfirmed = false;
                    receipt.IsSAP = false;
                    receipt.CreatedBy = LoginUserInfo.CurrentUser.LoginName;
                    receipt.Save();
                    break;
                case OperationType.Update:
                    receipt.LoadByPrimaryKey(receiptID.Value);
                    receipt.DocumentsNo = txtDocumentNo.Text.Trim();
                    receipt.DocumentsType = drpDocType.SelectedValue;
                    receipt.IsConfirmed = true;


                    receipt.Division = drpDivision.SelectedValue;
                    receipt.VoucherDescription = txtVocherDesc.Text;
                    receipt.VoucherDate = txtVoucherDate.Value;
                    receipt.FirstStorageDate = txtInDate.Value.ToDate();
                    receipt.CompanyCode = drpCompany.SelectedValue;

                    receipt.StorageLocation = decimal.Parse(drpLocation.SelectedValue);
                    receipt.StorageStatus = drpStorageStatus.SelectedValue;
                    receipt.FunctionDept = drpFunctionDept.SelectedValue;
                    receipt.Comments = txtComments.Text;
                    receipt.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    receipt.UpdatedDttm = DateTime.Now;
                    receipt.TransactionID = Guid.NewGuid().ToString();
                    receipt.VersionNo = receipt.VersionNo + 1;
                    receipt.Save();
                    break;
                case OperationType.Delete:
                    receipt.LoadByPrimaryKey(receiptID.Value);
                    if (receipt.IsSAP.HasValue && receipt.IsSAP.Value)
                    {
                        WebHelper.GotoParent("不能删除从SAP上传的数据!");
                    }
                    else
                    {
                        receipt.MarkAsDeleted();
                        receipt.Save();
                    }
                    break;
            }
            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }

    }
}